package cn.oa.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.oa.dao.UserMapper;
import cn.oa.pojo.User;

@Controller
public class UserController {
	
	@Resource(name = "userMapper")
	UserMapper um;
	/**
	 * 跳转至登录界面
	 * @return
	 */
	@RequestMapping("/toLogin")
	public String toLogin(HttpServletRequest request) {
		
		return "login";
		
	
	}
	/**
	 * 执行登录操作
	 * @param request
	 * @param user
	 * @return
	 */
	@RequestMapping("/doLogin")
	public String doLogin(HttpServletRequest request,User user) {
		//调用login方法查找用户
		User users = um.login(user);
		if(users!=null) {
			//将用户放到session中
			request.getSession().setAttribute("user", users);
			//重定向到index
			return "redirect:/index";
		}else {
			return "redirect:/toLogin";
		}
		
		
	}
	
	/**
	 * 跳转至主页面
	 * @return
	 */
	@RequestMapping("/index")
	public String index() {
		return "index";
	}
	
	
	
	
	/**
	 * 注册
	 */
	@RequestMapping("/doRegister")
	public String doRegister(HttpServletRequest request,User user) {
		int i = um.insert(user);
		if(i>0) {
			return "redirect:/toLogin";
		}else {
			return "redirect:/toLogin";
		}
		
	}
	

}
